package pl.un4giv3n.mightvsmagic.utils.display;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.jsp.PageContext;

import org.displaytag.decorator.TableDecorator;
import org.displaytag.model.TableModel;

import pl.un4giv3n.mightvsmagic.entity.ActivityDetails;
import pl.un4giv3n.mightvsmagic.entity.ActivityType;
import pl.un4giv3n.mightvsmagic.utils.wrappers.UserActivityWrapper;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.TextProviderFactory;

public class DisplayUserLogDecorator extends TableDecorator implements
LocaleProvider{

	private static SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
	private TextProvider resourceBundle;

	@Override
	public void init(PageContext pageContext, Object decorated,
			TableModel tableModel) {
		super.init(pageContext, decorated, tableModel);
		this.resourceBundle = new TextProviderFactory().createInstance(
				getClass(), this);

	}
	public String getTime(){
		UserActivityWrapper activity = (UserActivityWrapper) getCurrentRowObject();
		return sdf.format(new Date(activity.getTimeDone()));
	}
	
	public String getActivityType(){
		UserActivityWrapper activity = (UserActivityWrapper) getCurrentRowObject();
		return resourceBundle.getText(activity.getActivityType().name());
	}
	
	public String getDetails(){
		UserActivityWrapper activity = (UserActivityWrapper) getCurrentRowObject();
		ActivityDetails details = activity.getDetails();
		if(details != null && (activity.getActivityType() == ActivityType.ATTACK_OTHER_USER || activity.getActivityType() == ActivityType.WAS_ATTACKED_BY_USER)){
			StringBuilder link = new StringBuilder("<a target='_blank' href=showFightReport.action?fightId=");
			link.append(details.getParamId());
			link.append(">Pokaż</a>");
			return link.toString();
		}else{
			return "";
		}
	}
	
	public String getDescription(){
		UserActivityWrapper activity = (UserActivityWrapper) getCurrentRowObject();
		ActivityDetails details = activity.getDetails();
		return details == null ? "Brak informacji" : details.getDescription();
	}
	
	@Override
	public Locale getLocale() {
		ActionContext ctx = ActionContext.getContext();
		if (ctx != null) {
			return ctx.getLocale();
		} else {
			return Locale.getDefault();
		}
	}
}
